//
//  HMVisitorTableViewController.swift
//  Weibo
//
//  Created by Apple on 2017/7/11.
//  Copyright © 2017年 KY. All rights reserved.
//

import UIKit
//  访客视图控制器
class HMVisitorTableViewController: UITableViewController {
    
    //  是否登录的标记
    var isLogin: Bool = HMUserAccountViewModel.sharedUserAccount.isLogin
    //  访客视图
    var visitorView: HMVisitorView?
    
    //  重写loadView自定义视图
    override func loadView() {
        if isLogin {
            super.loadView()
        } else {
            //  没有登录，设置导航栏按钮
            //  提供自定义视图
            visitorView = HMVisitorView()
            //  设置代理对象
            visitorView?.delegate = self
            //  设置闭包
            visitorView?.loginCallBack = { [unowned self] in
                print("闭包回调的登录操作")
                self.showLogin()
            }
            view = visitorView
            setNavUI()
        }
    }
    
    //  设置导航栏按钮
    private func setNavUI() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", target: self, action: #selector(registerButtonAction))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", target: self, action: #selector(loginButtonAction))
    }
    
    //  MARK:   --点击事件
    @objc private func registerButtonAction() {
        print("register")
        showLogin()
    }
    
    @objc private func loginButtonAction() {
        print("login")
        showLogin()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    //  MARK:   --微博登录界面
    private func showLogin() {
        let oauthVc = HMOAuthViewController()
        let nav = HMNavigationController(rootViewController: oauthVc)
        present(nav, animated: true, completion: nil)
    }
    
    
    
}

extension HMVisitorTableViewController: HMVisitorViewDelegate {
    func loginOperation() {
        print("我是代理对象调用的登录方法")
    }
}
